package com.redteam.xomtro;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.redteam.xomtro.fragment.ProfileTab_About;
import com.redteam.xomtro.fragment.ProfileTab_Comment;
import com.redteam.xomtro.fragment.ProfileTab_Friend;
import com.redteam.xomtro.fragment.ProfileTab_Image;
import com.redteam.xomtro.fragment.ProfileTab_Room;

public class ProfileActivity extends MasterActivity implements ImageLoadingListener {

    private View lastSelected;
    private boolean isLoadedImage = false;
    private boolean isLoadedAvatar = false;
    private int menuId = R.menu.place_profile;
    private ImageView profileCover;
    private ImageView profileAvatar;
    private TextView profileName;
    private TextView profileSubDetail;
    private boolean isUserProfile = false;
    private String userAvatar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        String placeItem = getIntent().getStringExtra("place_item");
        String userItem = getIntent().getStringExtra("user_item");
        userAvatar = getIntent().getStringExtra("user_avatar");

        profileCover = (ImageView) findViewById(R.id.ivProfileCover);
        profileAvatar = (ImageView) findViewById(R.id.ivProfileAvatar);
        profileName = (TextView) findViewById(R.id.tvProfileName);
        profileSubDetail = (TextView) findViewById(R.id.tvProfileSubDetail);
        profileName.setTypeface(fontSLight);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WellcomeActivity.widthScreen / 4, ViewGroup.LayoutParams.MATCH_PARENT);
        if (userItem != null) {
            layoutParams = new LinearLayout.LayoutParams(WellcomeActivity.widthScreen / 3, ViewGroup.LayoutParams.MATCH_PARENT);
            initUserProfile(userItem, layoutParams);
        } else if (placeItem != null) {
            initPlaceProfile(placeItem, layoutParams);
        }
        lastSelected = findViewById(R.id.profileTab_event);
        lastSelected.setLayoutParams(layoutParams);
        lastSelected.setOnClickListener(this);
        findViewById(R.id.profileTab_about).setLayoutParams(layoutParams);
        findViewById(R.id.profileTab_about).setOnClickListener(this);
    }

    private void initUserProfile(String userItem, LinearLayout.LayoutParams layoutParams) {
        isUserProfile = true;
        menuId = R.menu.user_profile;
        setTitle(userItem + "'s Profile");
        View friendTab = findViewById(R.id.profileTab_friend);
        friendTab.setVisibility(View.VISIBLE);
        friendTab.setLayoutParams(layoutParams);
        friendTab.setOnClickListener(this);


        imageLoader.displayImage("http://lorempixel.com/960/500/nature", profileCover, options, this);
        if (userAvatar != null && userAvatar.length() > 0)
            imageLoader.displayImage(userAvatar, profileAvatar, optionsAvatar, this);
        else
            imageLoader.displayImage("https://lh3.googleusercontent.com/-P1jsQuDVO_4/AAAAAAAAAAI/AAAAAAAAAAA/0QUMOrSITm8/s96-c/photo.jpg", profileAvatar, optionsAvatar, this);
        profileName.setText(userItem);
    }

    private void initPlaceProfile(String placeItem, LinearLayout.LayoutParams layoutParams) {
        setTitle(placeItem + "'s Profile");
        View imageTab = findViewById(R.id.profileTab_image);
        imageTab.setVisibility(View.VISIBLE);
        imageTab.setLayoutParams(layoutParams);
        imageTab.setOnClickListener(this);
        View commentTab = findViewById(R.id.profileTab_comment);
        commentTab.setVisibility(View.VISIBLE);
        commentTab.setLayoutParams(layoutParams);
        commentTab.setOnClickListener(this);

        profileName.setText(placeItem);
        profileSubDetail.setText("Số 32, Đường D2, Bình Thạnh, TP.HCM");

        imageLoader.displayImage("http://lorempixel.com/960/500/nature", profileCover, options, this);
        imageLoader.displayImage("http://lorempixel.com/200/200/city", profileAvatar, optionsAvatar, this);
    }

    @Override
    public void onClick(View view) {
        if (lastSelected == view)
            return;
        switch (view.getId()) {
            case R.id.profileTab_event:
                beginTransaction(new ProfileTab_Room().newInstance(this));
                break;
            case R.id.profileTab_about:
                beginTransaction(new ProfileTab_About().newInstance(this, isUserProfile));
                break;
            case R.id.profileTab_image:
                beginTransaction(new ProfileTab_Image().newInstance(this));
                break;
            case R.id.profileTab_comment:
                beginTransaction(new ProfileTab_Comment().newInstance(this));
                break;
            case R.id.profileTab_friend:
                beginTransaction(new ProfileTab_Friend().newInstance(this));
                break;
        }
        unSelectTab(lastSelected);
        selectTab(view);
    }

    public void selectTab(View v) {
        TextView text = (TextView) ((ViewGroup) v).getChildAt(0);
        if (text != null)
            text.setTextColor(getResources().getColor(R.color.text_enable));
        View child = ((ViewGroup) v).getChildAt(1);
        if (child != null)
            child.setVisibility(View.VISIBLE);
        lastSelected = v;
    }

    public void unSelectTab(View v) {
        TextView text = (TextView) ((ViewGroup) v).getChildAt(0);
        if (text != null)
            text.setTextColor(getResources().getColor(R.color.text_disable));
        View child = ((ViewGroup) v).getChildAt(1);
        if (child != null)
            child.setVisibility(View.GONE);
    }

    @Override
    public void onLoadingFailed(String s, View view, FailReason failReason) {
        switch (view.getId()) {
            case R.id.ivProfileCover:
                isLoadedImage = true;
                break;
            case R.id.ivProfileAvatar:
                isLoadedAvatar = true;
                break;
        }
        if (isLoadedAvatar && isLoadedImage)
            beginTransaction(new ProfileTab_Room().newInstance(this));
    }

    @Override
    public void onLoadingComplete(String s, View view, Bitmap bitmap) {
        switch (view.getId()) {
            case R.id.ivProfileCover:
                isLoadedImage = true;
                break;
            case R.id.ivProfileAvatar:
                isLoadedAvatar = true;
                break;
        }
        if (isLoadedAvatar && isLoadedImage)
            beginTransaction(new ProfileTab_Room().newInstance(this));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(menuId, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_rating:
                Toast.makeText(this, "rating this location", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.action_direction:
                Toast.makeText(this, "open map direction", Toast.LENGTH_SHORT).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onLoadingStarted(String s, View view) {

    }

    @Override
    public void onLoadingCancelled(String s, View view) {

    }
}
